<template>
  <div class="wealth-purse">
    <ui-loading v-if="initLoading" class="page-init-loading">
      <div slot="content"></div>
    </ui-loading>
    <div class="fixed">
      <wealth-bg></wealth-bg>
      <div class="purse-top">
        <div class="banner">
          <div class="slide">
            <ul class="swiper-wrapper">
              <li
                class="purse-banner swiper-slide"
                v-for="item in purseBannerList"
                :key="item.currency_code"
                :data-code="item.currency_code"
              >
                <div class="banner-top">
                  <p class="banner-left">{{item.currency_name}}账户</p>
                  <div class="banner-right">
                    <span class="right-recharge" @click="recharge(item)">充值</span>
                    <span class="right-withdraw" @click="withdraw(item)">提现</span>
                  </div>
                </div>
                <div class="banner-mid">
                  <span class="mid-total">总金额</span>
                  <span class="mid-use">可用金额</span>
                </div>
                <div class="banner-bottom">
                  <span class="bottom-total">{{midDate.total}}</span>
                  <span class="bottom-use">{{midDate.usable}}</span>
                </div>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
    <div class="h428"></div>
    <ScrollList ref="scroll" :config="config" @bottom="getPage" @pull="pull">
      <div class="purse-mid">
        <div class="purse-mid-top" @click="getout">
          <div class="purse-mid-left">
            <p>提现中的金额</p>
            <span class>冻结</span>
          </div>
          <div class="purse-mid-right">
            <span>{{midDate.getout}}</span>
            <i class></i>
          </div>
        </div>
        <div class="purse-mid-line"></div>
        <div class="purse-mid-bottom" @click="unconfirm">
          <div class="purse-mid-left">
            <p>未确认订单总额</p>
            <span class="mid-tip">冻结</span>
          </div>
          <div class="purse-mid-right">
            <span>{{midDate.unconfirm}}</span>
            <i class></i>
          </div>
        </div>
      </div>
      <div class="purse-line"></div>
      <div class="purse-bottom">
        <p class="purse-bottom-title">账户流水</p>
        <div class="purse-bottom-line"></div>
        <div class="purse-bottom-list">
          <div
            class="purse-list-item"
            v-for="(item,index) in list"
            :key="index"
            @click="toToDetails(item)"
          >
            <div class="purse-item-left">
              <p>{{item.type_str}}</p>
              <p class="purse-item-time">{{item.add_time_str || item.addtime}}</p>
            </div>
            <p
              class="purse-item-right"
              :class="showAmountStyle(item.amount)"
            >{{item.amount | formataAmount}}{{item.currency_code}}</p>
          </div>
        </div>

        <ui-empty :empty="{text:'暂无数据'}" v-if="list.length==0"></ui-empty>
      </div>
    </ScrollList>
  </div>
</template>

<script>
import { mapMutations } from "vuex";
import Swiper from "swiper";
import WealthBg from "~/components/WealthBg.vue";
import UiEmpty from "~/components/empty";
import ScrollList from "~/components/ScrollList";
import UILoading from "~/components/loading";
export default {
  components: {
    WealthBg,
    ScrollList,
    UiEmpty,
    [UILoading.name]: UILoading
  },
  data: function() {
    return {
      purseBannerList: [],
      swiper: "",
      page: 1,
      total: 1,
      list: [],
      loading: false,
      config: {
        bottom: false,
        pull: false,
        end: false
      },
      currency_code: "",
      midDate: {
        symbol: "¥",
        name: "人民币",
        total: "0.00",
        usable: "0.00",
        getout: "0",
        unconfirm: " 0",
        total_convert: "0.00 CNY",
        usable_convert: "0.00 CNY",
        getout_convert: "0 CNY",
        unconfirm_convert: "0 CNY"
      },
      initLoading: true
    };
  },
  computed: {
    currencys() {
      return this.$store.state.currencys;
    }
  },
  mounted() {
    let that = this;
    (async () => {
      await this.getBanner();
    })();
    // this.$nextTick(() => {
    //   this.$refs.scroll.$emit("pulling");
    // });
    this.$nextTick(() => {
      setTimeout(() => {
        this.swiper = new Swiper(".slide", {
          slidesPerView: "auto",
          centeredSlides: !0,
          watchSlidesProgress: !0,
          pagination: ".swiper-pagination",
          paginationClickable: !0,
          initialSlide: this.idx,
          on: {
            slideChange: function() {
              var a = this.slides;
              that.currency_code = a[this.activeIndex].getAttribute(
                "data-code"
              );
              (async () => {
                await that.getList(that.currency_code);
              })();
              (async () => {
                that.$nuxt.$loading.start();
                that.page = 1;
                that.config.end = false;
                await that.getPage(that.currency_code, true);
              })();
            }
          }
        });
      }, 500);
    });
  },
  methods: {
    async getBanner() {
      this.initLoading = true;
      let { data: rs } = await this.$axios.post("?ct=api&ac=get_wallet");
      if (rs.code == 0) {
        let data = rs.data.wallets;
        // if (!Array.isArray(data) || data.length < 1) {
        //   this.$toast(rs.msg);
        //   return;
        // }

        this.purseBannerList = data;
        let allCurrencys =
          this.currencys || JSON.parse(sessionStorage.getItem("currencys"));
        let arr = [];
        let news = {};

        for (var i = 0; i < allCurrencys.length; i++) {
          let news = {};
          news.id = "";
          news.uid = "";
          news.currency_code = allCurrencys[i].code;
          news.user_type = "";
          news.currency_name = allCurrencys[i].name;
          news.total_amount = "0.00";
          news.frozen_amount = "0.00";
          news.uncommit_amount = "0.00";
          news.amount = "0.00";
          news.status = 1;
          arr.push(news);
        }

        for (var i = 0; i < arr.length; i++) {
          let name = arr[i].currency_name;
          let flag = true;
          for (var j = 0; j < data.length; j++) {
            if (name == data[j].currency_name) {
              flag = false;
            }
          }
          if (flag) {
            data.push(arr[i]);
          }
        }
        this.currency_code = data[0].currency_code;

        await Promise.all([
          this.getList(this.currency_code),
          this.getPage(this.currency_code)
        ]);

        setTimeout(() => {
          this.initLoading = false;
        }, 1000);
      } else {
        this.$toast(rs.msg);
      }
    },
    async getList(code, isRefresh) {
      try {
        let { data: rs } = await this.$axios.post(
          "?ct=api&ac=treasure_wallets",
          {
            currency_code: code
          }
        );
        if (rs.code == 0) {
          this.midDate = rs.data;
        } else {
          this.$toast(rs.msg);
        }
      } catch (e) {
        this.$error(e);
      }
      this.loading = false;
    },
    async getPage(code, isRefresh) {
      if (this.loading || this.config.end) return false;
      this.loading = true;
      try {
        let { data: rs } = await this.$axios.post("?ct=api&ac=payment_logs", {
          page: this.page,
          currency_code: code,
          from_type: 1
        });
        if (rs.code == 0) {
          const { data } = rs;
          this.total = Number(data.total);
          if (isRefresh) {
            this.list = data.list;
          } else {
            this.list = this.list.concat(data.list);
          }
          if (this.total > this.list.length) {
            this.page++;
            this.config.end = false;
          } else {
            this.config.end = true;
          }
        } else {
          this.$toast(rs.msg);
        }
      } catch (e) {
        this.$error(e);
      }
      this.loading = false;
    },
    async pull(cb) {
      this.$nuxt.$loading.start();
      this.page = 1;
      this.config.end = false;
      this.getList(this.currency_code);
      this.getPage(this.currency_code, true);
      this.$nuxt.$loading.finish();
      cb();
    },
    showAmountStyle(amount) {
      return {
        amount: amount < 0 ? true : false
      };
    },
    recharge(item) {
      if (item.status == 1) {
        this.$router.push({
          path: "/wealth/recharge",
          query: { code: item.currency_code }
        });
      } else {
        this.$toast("暂时无法充值");
      }
    },
    withdraw(item) {
      if (item.status == 1) {
        this.$router.push({
          path: "/wealth/draw",
          query: { code: item.currency_code }
        });
      } else {
        this.$toast("暂时无法提现");
      }
    },
    getout() {
      this.$router.push({ path: "/bill", query: { id: 5 } });
    },
    unconfirm() {
      this.$router.push({
        path: "/order",
        query: { tab: "my_uncommit", idx: 2 }
      });
    },
    toToDetails(item) {
      if (item.is_show == "1") {
        this.$router.push({
          path: "bill/bill-details",
          query: { bill_id: item.bill_id }
        });
      }
    }
  },
  filters: {
    formatDate(time) {
      return time;
    },
    formataAmount(amount) {
      if (amount > 0) amount = "+" + amount;
      return amount;
    }
  }
};
</script>

<style lang="less" scoped>
.fixed {
  position: fixed;
  top: 88px;
  left: 0;
  z-index: 999;
  width: 100%;
}
.h428 {
  height: 428px;
}
.swiper-wrapper {
  display: flex;
  transform-style: preserve-3d;
}
.purse-top {
  overflow: hidden;
  margin-top: -208px;
  height: 340px;
}
.purse-banner {
  line-height: 1;
  width: 708px;
  height: 340px;
  background: url("~assets/img/caifu_kapian.png") no-repeat;
  background-size: 708px 340px;
}
.banner-top {
  padding-top: 54px;
  width: 708px;
}
.banner-left {
  display: inline-block;
  margin-left: 74px;
  font-size: 42px;
  font-family: "PingFangSC-Medium";
}
.banner-right {
  float: right;
  display: inline-block;
  margin-right: 74px;
  line-height: 42px;
}
.banner-right span {
  font-family: "PingFangSC-Regular";
  font-size: 28px;
  color: rgb(32, 134, 249);
}
.right-recharge {
  margin-right: 48px;
}
.right-withdraw {
}
.banner-mid {
  margin-top: 84px;
  font-size: 24px;
  color: rgb(153, 153, 153);
  font-family: "PingFangSC-Regular";
}
.mid-total {
  margin-left: 74px;
}
.mid-use {
  margin-left: 238px;
}
.banner-bottom {
  margin-top: 24px;
  font-size: 42px;
  color: rgb(41, 41, 41);
  font-family: "SFUIDisplay-Medium";
}
.bottom-total {
  position: absolute;
  margin-left: 72px;
}
.bottom-use {
  position: absolute;
  margin-left: 388px;
}

.purse-mid {
  height: 188px;
}
.purse-mid-top {
  height: 94px;
}
.purse-mid-left {
  position: absolute;
  margin-left: 30px;
  line-height: 94px;
}
.purse-mid-left p {
  float: left;
  font-family: "PingFangSC-Regular";
  font-size: 30px;
  color: rgb(102, 102, 102);
}
.purse-mid-left span {
  float: left;
  margin-left: 10px;
  margin-top: 20px;
  width: 54px;
  height: 28px;
  font-family: "PingFangSC-Medium";
  font-size: 18px;
  color: rgb(255, 255, 255);
  line-height: 28px;
  background: url("~assets/img/bg_business_bubble@2x.png") no-repeat;
  background-size: 54px 28px;
  -webkit-text-size-adjust: none;
}
.purse-mid-left .mid-tip {
  /* left: 218px; */
}
.purse-mid-right {
  position: absolute;
  right: 30px;
  line-height: 94px;
}
.purse-mid-right span {
  display: inline-block;
  margin-right: 10px;
  color: rgb(41, 41, 41);
  font-size: 28px;
  font-family: "SFUIDisplay-Medium";
  -webkit-text-size-adjust: none;
}
.purse-mid-right i {
  display: inline-block;
  width: 10px;
  height: 18px;
  background: url("~assets/img/jiantou_right@2x.png") no-repeat;
  background-size: 10px 18px;
}
.purse-mid-line {
  margin-left: 30px;
  /* height: 2px; */
  border-bottom: 2px solid #ededf0;
}
.purse-mid-bottom {
  height: 94px;
}

.purse-line {
  height: 22px;
  background-color: #ededf0;
}

.purse-bottom {
}
.purse-bottom-title {
  height: 88px;
  line-height: 88px;
  font-family: "PingFangSC-Medium";
  font-size: 32px;
  color: rgb(41, 41, 41);
  margin-left: 30px;
}
.purse-bottom-line {
  border-bottom: 2px solid #ededf0;
}
.purse-bottom-list {
}
.purse-list-item {
  padding:24px 30px;
  display: flex;
}
.purse-item-left {
  flex:1;
}
.purse-item-left p {
  .line-clamp();
  line-height: 44px;
  font-size: 28px;
}
.purse-item-left .purse-item-time {
  margin-top:20px;
  color: rgb(153, 153, 153);
  font-size: 24px;
}
.purse-item-right {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  margin-left: 20px;
  font-size: 28px;
}
.purse-item-right.amount {
  color: #f05521;
}
.purse-bottom-item-clear {
  clear: both;
}
.purse-bottom-tips {
  line-height: 60px;
  text-align: center;
  font-size: 24px;
  color: #c4c6cb;
  font-family: "PingFangSC-Regular";
  background-color: #ecefef;
}
.purse-bottom .empty {
  padding-bottom: 100px;
  margin-top: 100px;
}
</style>
